<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Haotian extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'user'  => $bean->getSupplierNum(), //商户密钥
            'pass'  => $bean->getSecretKey(),
            'spid'  => $bean->getGoodsId(), //商品编码
            'money' => $bean->getUnitPrice() * $bean->getStartCharging(),
            'mun'   => $bean->getRechargeNum() * $bean->getStartCharging(), //充值数量,
            'beizhu'=> base64_encode(json_encode([$bean->getAccount()])),
            'yibu'  => $bean->getNotifyUrl(), //异步回调地址
            'code'  => '102',
            'order' => $bean->getOrderNo(), //订单号
        ];
        ksort($array);
        $array['token'] = md5('user='.$array['user'].'&&pass='.$array['pass']);
        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('type', '');
        $order_sn = $request->input('order', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['4', '5']))->setRechargeSuccess($status == '4')->setIsOrderNo(false);
    }
}
